﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using HP.Cachalote.DataModels;
using HP.Cachalote.WPF.ViewModel;
using HP.Cachalote.WPF.Shared;

namespace HP.Cachalote.WPF.UserControls
{
    /// <summary>
    /// Interaction logic for TodoLabelTreeViewUserControl.xaml
    /// </summary>
    public partial class TodoLabelTreeViewUserControl : UserControlBase
    {
        public TodoLabelTreeViewUserControl()
        {
            InitializeComponent();
        }

        protected override void RegisterMessages()
        {
            base.RegisterMessages();
            RegisterDeleteLabelConfirmMessage();
        }

        /// <summary>
        /// Get message from TodoLabelTreeViewItemViewModel to show a deletion confirm dialog.
        /// </summary>
        private void RegisterDeleteLabelConfirmMessage()
        {
            Messenger.Default.Register<DialogMessage>(this, TodoLabelTreeViewItemViewModel.DeleteLabelConfirmMessageToken, (dialog) =>
            {
                //var confirmResult = MessageBox.Show(dialog.Content, dialog.Caption, dialog.Button, dialog.Icon);

                var dialogWindow = new DialogWindow(dialog.Content);
                dialogWindow.ShowDialog();

                var confirmResult = dialogWindow.Result;
                dialog.ProcessCallback(confirmResult);
            });
        }

        private void TreeViewLabelsItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;

            if (item != null)
            {
                item.IsSelected = true;
            }
        }

        private void TreeViewLabelsItem_LostFocus(object sender, RoutedEventArgs e)
        {
            /*
             Have no idea why the Trigger in xaml does not work.
             * 
            <Style.Triggers>
	            <Trigger Property="IsFocused" Value="False">
		            <Setter Property="IsSelected" Value="False" />
	            </Trigger>
            </Style.Triggers>
             */

            var item = sender as TreeViewItem;
            item.IsSelected = false;
        }
    }
}
